﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YelpAPI.Objects;

namespace YelpAPI
{
    public static class YelpSearchService
    {
        private const Int32 MaxResultCount = 5;
        private const String RequestUrl = "http://api.yelp.com/business_review_search";

        public static SearchResult SearchBoundingBox(Double topLatitude, Double leftLongitude, Double bottomLatitude, Double rightLongitude, String category, String term)
        {
            return SearchBoundingBox(topLatitude, leftLongitude, bottomLatitude, rightLongitude, category, term, MaxResultCount);
        }

        public static SearchResult SearchBoundingBox(Double topLatitude, Double leftLongitude, Double bottomLatitude, Double rightLongitude, String category, String term, Int32 resultCount)
        {
            YelpRequest<SearchResult> request = new YelpRequest<SearchResult>(RequestUrl);

            request.Parameters.Add("tl_lat", topLatitude.ToString());
            request.Parameters.Add("tl_long", leftLongitude.ToString());
            request.Parameters.Add("br_lat", bottomLatitude.ToString());
            request.Parameters.Add("br_long", rightLongitude.ToString());

            if (!String.IsNullOrEmpty(category))
            {
                request.Parameters.Add("category", category);
            }

            if (!String.IsNullOrEmpty(term))
            {
                request.Parameters.Add("term", term);
            }

            request.Parameters.Add("num_biz_requested", resultCount.ToString());

            return request.ExecuteRequest();
        }

        public static SearchResult SearchRadius(Double latitude, Double longitude, String category, String term)
        {
            return SearchRadius(latitude, longitude, category, term, MaxResultCount);
        }

        public static SearchResult SearchRadius(Double latitude, Double longitude, String category, String term, Int32 resultCount)
        {
            YelpRequest<SearchResult> request = new YelpRequest<SearchResult>(RequestUrl);

            request.Parameters.Add("lat", latitude.ToString());
            request.Parameters.Add("long", longitude.ToString());

            if (!String.IsNullOrEmpty(category))
            {
                request.Parameters.Add("category", category);
            }

            if (!String.IsNullOrEmpty(term))
            {
                request.Parameters.Add("term", term);
            }

            request.Parameters.Add("num_biz_requested", resultCount.ToString());

            return request.ExecuteRequest();
        }

        public static SearchResult SearchRadius(Double latitude, Double longitude, Double radius, String category, String term)
        {
            return SearchRadius(latitude, longitude, radius, category, term, MaxResultCount);
        }

        public static SearchResult SearchRadius(Double latitude, Double longitude, Double radius, String category, String term, Int32 resultCount)
        {
            YelpRequest<SearchResult> request = new YelpRequest<SearchResult>(RequestUrl);

            request.Parameters.Add("lat", latitude.ToString());
            request.Parameters.Add("long", longitude.ToString());
            request.Parameters.Add("radius", radius.ToString());

            if (!String.IsNullOrEmpty(category))
            {
                request.Parameters.Add("category", category);
            }

            if (!String.IsNullOrEmpty(term))
            {
                request.Parameters.Add("term", term);
            }

            request.Parameters.Add("num_biz_requested", resultCount.ToString());

            return request.ExecuteRequest();
        }

        public static SearchResult SearchLocation(String location, String category, String term)
        {
            return SearchLocation(location, category, term, MaxResultCount);
        }

        public static SearchResult SearchLocation(String location, String category, String term, Int32 resultCount)
        {
            YelpRequest<SearchResult> request = new YelpRequest<SearchResult>(RequestUrl);

            request.Parameters.Add("location", location);

            if (!String.IsNullOrEmpty(category))
            {
                request.Parameters.Add("category", category);
            }

            if (!String.IsNullOrEmpty(term))
            {
                request.Parameters.Add("term", term);
            }

            request.Parameters.Add("num_biz_requested", resultCount.ToString());

            return request.ExecuteRequest();
        }

        public static SearchResult SearchLocation(String location, Double radius, String category, String term)
        {
            return SearchLocation(location, category, term, MaxResultCount);
        }

        public static SearchResult SearchLocation(String location, Double radius, String category, String term, Int32 resultCount)
        {
            YelpRequest<SearchResult> request = new YelpRequest<SearchResult>(RequestUrl);

            request.Parameters.Add("location", location);
            request.Parameters.Add("radius", radius.ToString());

            if (!String.IsNullOrEmpty(category))
            {
                request.Parameters.Add("category", category);
            }

            if (!String.IsNullOrEmpty(term))
            {
                request.Parameters.Add("term", term);
            }

            request.Parameters.Add("num_biz_requested", resultCount.ToString());

            return request.ExecuteRequest();
        }

        public static SearchResult SearchHood(String neighborhood)
        {
            return SearchHood(neighborhood, MaxResultCount);
        }

        public static SearchResult SearchHood(String neighborhood, Int32 resultCount)
        {
            YelpRequest<SearchResult> request = new YelpRequest<SearchResult>(RequestUrl);

            request.Parameters.Add("location", neighborhood);
            request.Parameters.Add("num_biz_requested", resultCount.ToString());

            return request.ExecuteRequest();
        }
    }
}
